package com.ruoyi.qrCode.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.distributionPoster.domain.QRCodeRequest;
import com.ruoyi.distributionPoster.domain.QRCodeResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.qrCode.domain.QrCode;
import com.ruoyi.qrCode.service.IQrCodeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.qrCode.domain.BusinessQRCodeRequest;
import com.ruoyi.qrCode.domain.BusinessQRCodeResult;

/**
 * 二维码日志管理Controller
 * 
 * @author ruoyi
 * @date 2025-08-02
 */
@RestController
@RequestMapping("/ruoyi/qrCode")
public class QrCodeController extends BaseController
{
    @Autowired
    private IQrCodeService qrCodeService;

    /**
     * 查询二维码日志管理列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:qrCode:list')")
    @GetMapping("/list")
    public TableDataInfo list(QrCode qrCode)
    {
        startPage();
        List<QrCode> list = qrCodeService.selectQrCodeList(qrCode);
        return getDataTable(list);
    }

    /**
     * 导出二维码日志管理列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:qrCode:export')")
    @Log(title = "二维码日志管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QrCode qrCode)
    {
        List<QrCode> list = qrCodeService.selectQrCodeList(qrCode);
        ExcelUtil<QrCode> util = new ExcelUtil<QrCode>(QrCode.class);
        util.exportExcel(response, list, "二维码日志管理数据");
    }

    /**
     * 获取二维码日志管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:qrCode:query')")
    @GetMapping(value = "/{qrCodeId}")
    public AjaxResult getInfo(@PathVariable("qrCodeId") Long qrCodeId)
    {
        return success(qrCodeService.selectQrCodeByQrCodeId(qrCodeId));
    }

    /**
     * 新增二维码日志管理
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:qrCode:add')")
    @Log(title = "二维码日志管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QrCode qrCode)
    {
        return toAjax(qrCodeService.insertQrCode(qrCode));
    }

    /**
     * 修改二维码日志管理
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:qrCode:edit')")
    @Log(title = "二维码日志管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QrCode qrCode)
    {
        return toAjax(qrCodeService.updateQrCode(qrCode));
    }

    /**
     * 删除二维码日志管理
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:qrCode:remove')")
    @Log(title = "二维码日志管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{qrCodeIds}")
    public AjaxResult remove(@PathVariable Long[] qrCodeIds)
    {
        return toAjax(qrCodeService.deleteQrCodeByQrCodeIds(qrCodeIds));
    }

    /**
     * 生成业务商品分享二维码
     * @param request 业务商品二维码请求参数
     * @return 二维码生成结果
     */
    @PostMapping("/generateBusinessQRCode")
    public AjaxResult generateBusinessQRCode(@RequestBody BusinessQRCodeRequest request) {
        try {
            BusinessQRCodeResult result = qrCodeService.generateBusinessQRCode(request);
            return AjaxResult.success("业务商品二维码生成成功", result);
        } catch (Exception e) {
            logger.error("生成业务商品二维码失败", e);
            return AjaxResult.error("业务商品二维码生成失败：" + e.getMessage());
        }
    }

    /**
     * 生成包含员工信息和业务商品信息的二维码
     * @param qrCodeRequest 二维码请求参数
     * @return 二维码生成结果
     */
    @PostMapping("/generateSkuQRCode")
    public AjaxResult generateSkuQRCode(@RequestBody BusinessQRCodeRequest qrCodeRequest) {
        try {
            // 验证参数
            if (qrCodeRequest.getSkuId() == null) {
                return AjaxResult.error("业务商品ID不能为空");
            }

            // 验证并设置默认尺寸
            if (qrCodeRequest.getWidth() != null && (qrCodeRequest.getWidth() < 100 || qrCodeRequest.getWidth() > 1000)) {
                return AjaxResult.error("二维码宽度必须在100-1000像素之间");
            }

            if (qrCodeRequest.getHeight() != null && (qrCodeRequest.getHeight() < 100 || qrCodeRequest.getHeight() > 1000)) {
                return AjaxResult.error("二维码高度必须在100-1000像素之间");
            }

            // 验证versionType参数
            if (qrCodeRequest.getVersionType() != null &&
                    !("test".equals(qrCodeRequest.getVersionType()) ||
                            "development".equals(qrCodeRequest.getVersionType()) ||
                            "production".equals(qrCodeRequest.getVersionType()))) {
                return AjaxResult.error("版本类型只能是: production(线上版), test(体验版), development(开发版)");
            }

            // 调用服务生成二维码
            BusinessQRCodeResult result = qrCodeService.generateSkuQRCode(qrCodeRequest);
            return AjaxResult.success("二维码生成成功", result);

        } catch (Exception e) {
            logger.error("生成商品二维码失败", e);
            return AjaxResult.error("二维码生成失败：" + e.getMessage());
        }
    }
}
